package com.yykj.weifenxiao.server.controller;

import com.yykj.common.vo.ResultVO;
import com.yykj.drup.client.WiFenXiaoOrder;
import com.yykj.drup.client.form.WiFenXiaoPayOrderFrom;
import com.yykj.weifenxiao.server.model.WIProduct;
import com.yykj.weifenxiao.server.model.WIStore;
import com.yykj.weifenxiao.server.service.AccessTokenService;
import com.yykj.weifenxiao.server.service.WiFenXiaoOrderService;
import com.yykj.weifenxiao.server.service.WiFenXiaoProductService;
import com.yykj.weifenxiao.server.service.WiFenXiaoStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author zeng
 * @Date 2022/3/8
 */
@RestController
@Api(tags = "微分销对接服务")
@RequestMapping("/wifenxiao")
@AllArgsConstructor
public class WifenxiaoController {

    final AccessTokenService accessTokenService;
    final WiFenXiaoStoreService wiFenXiaoStoreService;
    final WiFenXiaoProductService wiFenXiaoProductService;
    final WiFenXiaoOrderService wiFenXiaoOrderService;

    @ApiOperation("获取微分销access_token")
    @GetMapping("/accessToken/getToken")
    public String getToken(){
        return accessTokenService.getAccessToken();
    }

    @ApiOperation("持久化微分销门店列表")
    @GetMapping("/store/insertSysDrup")
    public ResultVO<Boolean> insertSysDrup(){
        List<WIStore> wiStoreList = wiFenXiaoStoreService.storeList();
        if (!wiStoreList.isEmpty()){
            wiFenXiaoStoreService.insertOrUpdateStore(wiStoreList);
        }
        return ResultVO.success(wiStoreList);
    }


    @ApiOperation("持久化微分销商品列表")
    @GetMapping("/product/insertSysDrup")
    public ResultVO<Boolean> productList(){
        List<WIProduct> wiProductList = wiFenXiaoProductService.productList();
        if(!wiProductList.isEmpty()){
            wiFenXiaoProductService.insertOrUpdateProduct(wiProductList);
        }
        return ResultVO.success(wiProductList);
    }

    @ApiOperation("获取微分销商城-门店列表")
    @GetMapping("/store/list")
    public ResultVO getStoreList(){
        return ResultVO.success(wiFenXiaoStoreService.storeList());
    }

    @ApiOperation("获取微分销商城-门店信息")
    @GetMapping("/store/selectByUserId")
    public ResultVO selectByUserId(Integer userId){
        return ResultVO.success(wiFenXiaoStoreService.selectStoreByUserId(userId));
    }

    @ApiOperation("获取微分销商城-商品列表")
    @GetMapping("/product/list")
    public ResultVO getProductList(){
        return ResultVO.success(wiFenXiaoProductService.productList());
    }

    @ApiOperation("微分销商城-门店下单")
    @PostMapping("/order/payOrder")
    public ResultVO<WiFenXiaoOrder> payOrder(@RequestBody WiFenXiaoPayOrderFrom wiFenXiaoPayOrderFrom){
        WiFenXiaoOrder orer = wiFenXiaoOrderService.wiFenXiaopayOrder(wiFenXiaoPayOrderFrom);
        return ResultVO.success(orer);
    }

    @Deprecated
    @ApiOperation("微分销商城-查询订单")
    @GetMapping("/order/findOrder")
    public WiFenXiaoOrder findOrder(@RequestParam("orgOrderNu") String orgOrderNu){
        WiFenXiaoOrder wiFenXiaoOrder = wiFenXiaoOrderService.wiFenXiaoFindOrder(orgOrderNu);
        return wiFenXiaoOrder;
    }


}
